import qrcode from './qrcode.js'
export const commonScan = (callback) => {
	// #ifndef H5
	uni.scanCode({
		onlyFromCamera: false,
		autoZoom: true,
		success: (res) => {
			console.log(res, 'res')
			if (res.result) {

				let scanResult = res.result;
				callback && callback.call(undefined, {
					scanResult: scanResult
				})
			}
		},
		fail: (err) => {
			uni.showToast({
				duration: 3000,
				title: err
			})
		}
	});
	// #endif
	// #ifdef H5
	uni.chooseImage({
		sizeType: ['original'],
		sourceType: ['camera', 'album'],
		count: 1,
		success: function(res) {
			console.log(res, 'res')
			const tempFilePaths = res.tempFilePaths[0] // 获取到二维码图片的链接
			qrcode.decode(tempFilePaths); // 解析二维码图片
			qrcode.callback = function(res1) {
				// 解析失败返回 error decoding QR Code
				if (res1 == "error decoding QR Code") {
					uni.showToast({
						title: "识别二维码失败，请重新上传！",
						duration: 2000,
						icon: 'none'
					})
				} else {
					// 解析成功返回二维码链接
					console.log(res1)
					callback && callback.call(undefined, {
						scanResult: res1
					})
				}
			}
		}
	});
	// #endif

}